/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.mala.service;

import br.jm.mala.generic.DAOGeneric;
import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author joao
 * @param <T>
 */
public abstract class GenericService<T> {
    private final DAOGeneric<T> dao;
 
    GenericService(DAOGeneric<T> dao) {
        this.dao = dao;
    }
    @Transactional(readOnly = true)
    public T login(String username, String password) throws IllegalArgumentException {
        if (isEmptyOrNull(username) || isEmptyOrNull(password)) {
            throw new IllegalArgumentException("Atenção, username ou password vazios!");
        }
        T u = (T) dao.login(username, password);

        if (u == null) {
            throw new IllegalArgumentException("Erro: username ou password incorretos!");
        }
        return u;
    }

    private boolean isEmptyOrNull(String s) {
        return s == null || s.equals("");
    }
    
    @Transactional
    public void create(T Entidade) {
        dao.create(Entidade);
    }
    
    @Transactional
    public void edit(T entidade) {
        dao.edit(entidade);
    }

    @Transactional
    public void destroy(T entidade) {
        dao.destroy(entidade);
    }

    @Transactional
    public void executeUpdate(String oql) {
        dao.executeUpdate(oql);
    }

    @Transactional(readOnly = true)
    public T getPorId(Object id) {
        return dao.getPorId(id);

    }

    @Transactional(readOnly = true)
    public List<T> get(String oql) {
        return dao.get(oql);
    }

    @Transactional(readOnly = true)
    public List<T> get(String oql, List<String> campos) {
        return dao.get(oql, campos);
    }

    @Transactional(readOnly = true)
    public List<T> get(String oql, Object... campos) {
        return dao.get(oql, campos);
    }

    @Transactional(readOnly = true)
    public List<T> getPorQuery(String query) {
        return dao.getPorQuery(query);
    }

    @Transactional(readOnly = true)
    public List<T> getPorQuery(String query, Object... campos) {
        return dao.getPorQuery(query, campos);
    }

    @Transactional(readOnly = true)
    public int getCount(String entidade) {
        return dao.getCount(entidade);
    }

    @Transactional(readOnly = true)
    public int getCount(String entidade, String where) {
        return dao.getCount(entidade, where);
    }

    @Transactional(readOnly = true)
    public int getCountQuery(String oql) {
        return dao.getCountQuery(oql);
    }

    @Transactional(readOnly = true)
    public List<T> getNative(String cTabela, int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters, String cParametroSQL) {
        return dao.getNative(cTabela, first, pageSize, sortField, sortOrder, filters, cParametroSQL);
    }

    @Transactional(readOnly = true)
    public List<T> getNative(String cTabela, int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters, String cParametroSQL, String cFiltro) {
        return dao.getNative(cTabela, first, pageSize, sortField, sortOrder, filters, cParametroSQL, cFiltro);
    }
    
    @Transactional(readOnly = true)
    public List<T> getNativeFiltroList(String cSql) {
        return dao.getNativeFiltroList(cSql);
    }

    @Transactional(readOnly = true) 
    public T getNativeFiltro(String cSql) {
        return dao.getNativeFiltro(cSql);
    }

    @Transactional(readOnly = true)
    public T getEntidade(String oql, List<String> campos) {
        return dao.getEntidade(oql, campos);
    }

    @Transactional(readOnly = true)
    public List<T> loadAll() {
        return dao.loadAll();
    }
}
